﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.building
{
    partial class SangoBuildingHandler
    {
        public building_queuebuy building_queuebuy()
        {
            string userCode = SangoManager.Authentication();

            building_queuebuy response = ReflectionManager.CreatePojo<building_queuebuy>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);

            Entities.Sys_Vipsetting vipsetting = SysVipSettingHelper.search(profile.Viplevel);

            int currentQueueCount = UsrQueueDao.getUsrQueueCount(userCode, CtiQueueTypecodeCollection.BUILDING_UPGRADE);

            if (currentQueueCount >= vipsetting.Buildingqueueqty)
            {
                response.Message = "VIP等级不足，无法继续扩展队列";
                return response;
            }

            Entities.Sys_Queue sysQueue = UsrQueueDao.getAvailableSysQueue(userCode, CtiQueueTypecodeCollection.BUILDING_UPGRADE);

            if (sysQueue == null)
            {
                response.Message = "无可扩展的队列";
                return response;
            }


            long goldprice = sysQueue.Goldprice;

            if (!UsrProfileDao.tryDecreaseGoldValue(profile, goldprice))
            {
                response.Message = "金币不足";
                return response;
            }

            Entities.Usr_Queue queue = UsrQueueDao.createQueue(profile.Usercode, profile.Nickname, sysQueue);

            property.Buildingcount = property.Buildingcount + 1;

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(profile);

                command.Insert(queue);

                command.Update(property);

                command.Commit();
            });


            response.queue = UsrQueueFacade.ToQueue(queue);
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.property = UsrProfileFacade.ToProperty(property);
            response.Succeed = true;
            return response;
        }


    }
    public interface building_queuebuy : ISangoResponse
    {
        player_queue queue { get;set;}
        player_profile profile { get;set;}
        player_property property { get;set;}
    }
}
